package com.gulimall.seckill.controller;

import com.common.utils.R;
import com.gulimall.seckill.service.Seckillservice;
import com.gulimall.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SeckillController {

    @Autowired
    Seckillservice seckillservice;


    /**
     * 立即秒杀
     */
    @GetMapping("/kill")
    public String sckill(@RequestParam("killId") String killId, @RequestParam("key") String key,
                         @RequestParam("num") Integer num, Model model){
        //有登录信息后
        String orderSn =  seckillservice.sckill(killId,key,num);
        model.addAttribute("orderSn",orderSn);
        return "success";
    }


    /**
     * 获取当前商品的秒杀信息
     * @param skuId
     * @return
     */
    @GetMapping("/sku/seckill/{skuId}")
    @ResponseBody
    public R getSkuSerkillInfo(@PathVariable("skuId") Long skuId){

        SeckillSkuRedisTo seckillSkuRedisTo = seckillservice.getSkuSerkillInfo(skuId);
        return R.ok().setData(seckillSkuRedisTo);
    }





    /**
     * 返回当前时间可以参与秒杀的商品信息
     * @return
     */
    @GetMapping("/currentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus(){
        List<SeckillSkuRedisTo> skuRedisTos =  seckillservice.getCurrentSeckillSkus();
       return R.ok().setData(skuRedisTos);
    }
}
